<?php

namespace Biz\tools;

use Doraemon\tools\HhzClient;

class  GoodsExposureLog
{
    const LOG_PATH = '/data/logs_bak/seaslog';
    const LOG_DIR = 'goods_exposure';
    const LOG_DEFAULE_SEPARATOR = '|';

    public static function addLog($request, $data, $requestParams="")
    {
        \SeasLog::setBasePath(self::LOG_PATH);
        \SeasLog::setLogger(self::LOG_DIR);
        $goodsList = self::getGoodsList($data);
        if ($goodsList) {
            $time = date('Y-m-d H:i:s');
            foreach ($goodsList as $goods) {
                $formatGoods = array_map(function ($data) {
                    return str_replace(self::LOG_DEFAULE_SEPARATOR, '-', $data);
                }, [
                    'page' => strtolower(trim($request->getRequestUri(), '/')),
                    'time' => $time,
                    'goods_id' => $goods['id'],
                    'uid' => HhzClient::getUid(),
                    'vid' => HhzClient::getVid(),
                    'shop_id' => $goods['shop_id'],
                    'category_id' => $goods['category_id'],
                    'second_category_id' => $goods['second_category_id'],
                    'request_params' => $requestParams,
                    'source' => HhzClient::getClientType()
                ]);
                \SeasLog::info(implode(self::LOG_DEFAULE_SEPARATOR, $formatGoods), array());
            }
        }
    }

    private static function getGoodsList($data)
    {
        $goodsList = [];
        foreach ($data as $temp) {
            if (is_array($temp)) {
                if (isset($temp['id']) && isset($temp['actual_min_price']) && isset($temp['cover_img']) && isset($temp['shop_id']) && isset($temp['category_id']) && isset($temp['second_category_id'])) {
                    $goodsList[] = $temp;
                } else {
                    $goodsList = array_merge($goodsList, self::getGoodsList($temp));
                }
            }
        }
        //var_dump($goodsList);
        return $goodsList;
    }
}